<template>
  <div class="forgetpass">
    <div class="card">
      <p style="font-size:20px">修改您的账户密码</p>
      <v-form ref="form" v-model="valid" lazy-validation>
        <v-text-field
          label="请输入您的手机号"
          outlined
          dense
          height="40px"
          v-model="form.phone"
          :rules="phoneRules"
          required
        ></v-text-field>
        <div class="yzm_box">
          <v-text-field
            label="请输入手机验证码"
            outlined
            height="40px"
            dense
            v-model="form.phone_code"
            :rules="phonecodeRules"
            required
          ></v-text-field>
          <v-btn
            rounded
            color="primary"
            dark
            width="120px"
            style="margin-left:10px"
            @click="getSms"
            :class="{ 'disabled-style': getCodeBtnDisable }"
            :disabled="isdisabled"
          >
            {{ codeBtnWord }}
          </v-btn>
        </div>
        <v-text-field
          label="登录密码"
          outlined
          dense
          v-model="form.password"
          :rules="passwordRules"
          required
        ></v-text-field>
        <v-text-field
          label="确认密码"
          outlined
          dense
          v-model="form.password2"
          :rules="passwordRules2"
          required
        ></v-text-field>
      </v-form>
      <div class="login_info">
        <v-btn text small color="primary"
          ><router-link to="/user/login">返回登录</router-link></v-btn
        >
      </div>
      <v-btn
        rounded
        color="primary"
        dark
        width="200px"
        style="margin-top:15px"
        @click="doEditPass"
      >
        修 改
      </v-btn>
    </div>
  </div>
</template>

<script>
import { doEditPass, getSms } from "@/api/login";
export default {
  name: "forgetpass",
  data() {
    return {
      valid: true,
      // imgurl: "",
      form: {
        phone: "",
        phone_code: "",
        password: "",
        password2: ""
      },
      phoneRules: [
        v => !!v || "手机号不能为空!"
        // v => (v && v.length <= 10) || "Name must be less than 10 characters"
      ],
      phonecodeRules: [
        v => !!v || "手机验证码不能为空!"
        // v => (v && v.length <= 10) || "Name must be less than 10 characters"
      ],
      passwordRules: [
        v => !!v || "密码不能为空!"
        // v => (v && v.length <= 10) || "Name must be less than 10 characters"
      ],
      passwordRules2: [
        v => !!v || "确认密码不能为空!",
        v => {
          let falg = v == this.form.password ? true : false;
          return falg || "两次密码输入不一样";
        }
      ],
      isdisabled: false,
      codeBtnWord: "获取验证码", // 获取验证码按钮文字
      waitTime: 61 // 获取验证码按钮失效时间
    };
  },

  computed: {
    // 控制获取验证码按钮是否可点击
    getCodeBtnDisable: {
      get() {
        if (this.waitTime == 61) {
          if (this.form.phone) {
            return false;
          }
          return true;
        }
        return true;
      },
      // 注意：因为计算属性本身没有set方法，不支持在方法中进行修改，而下面我要进行这个操作，所以需要手动添加
      set() {}
    }
  },
  created() {},
  mounted() {},
  methods: {
    // 获取手机验证码
    getSms() {
      if (this.form.phone !== "") {
        getSms({
          phone: this.form.phone
        })
          .then(res => {
            if (res.code === 200) {
              console.log("返回", res);
              let that = this;
              that.waitTime--;
              that.getCodeBtnDisable = true;
              this.codeBtnWord = `${this.waitTime}s 后重新获取`;
              let timer = setInterval(function() {
                if (that.waitTime > 1) {
                  that.waitTime--;
                  that.codeBtnWord = `${that.waitTime}s 后重新获取`;
                } else {
                  clearInterval(timer);
                  that.codeBtnWord = "获取验证码";
                  that.getCodeBtnDisable = false;
                  that.waitTime = 61;
                }
              }, 1000);
            } else {
              this.$message.info(res.msg);
            }
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        this.$message({
          type: "info",
          message: "请输入手机号"
        });
      }
    },
    // 忘记密码
    doEditPass() {
      if (
        this.form.phone !== "" &&
        this.form.phone_code !== "" &&
        this.form.password !== ""
      ) {
        doEditPass(this.form)
          .then(res => {
            if (res.code === 200) {
              console.log("返回", res);
              this.$message({
                type: "success",
                message: "修改成功"
              });
              this.$router.push({
                path: "/user/login"
              });
            } else {
              this.$message({
                type: "info",
                message: res.msg
              });
            }
          })
          .catch(err => {
            console.log(err);
          });
      } else {
        this.$message({
          type: "info",
          message: "请检查表单"
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.forgetpass::v-deep {
  .v-messages__message {
    font-size: 13px;
    line-height: 15px;
  }
  .card {
    width: 100%;
    height: 100%;
    background-color: #fff;
    border-radius: 8px;
    box-sizing: border-box;
    padding: 12px 20px;
    .login_info {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .yzm_box {
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
    }
  }
}
</style>
